<template>
  <div class="carousel-container">
    <!-- 轮播图主体 -->
    <div class="carousel-wrapper" :style="{ transform: `translateX(-${currentIndex * 100}%)` }">
      <div 
        class="carousel-slide" 
        v-for="(slide, index) in slides" 
        :key="index"
      >
        <img 
          :src="slide.imageUrl" 
          :alt="slide.altText" 
          class="slide-image"
        >
        <div class="slide-caption">
          <h3 class="caption-title">{{ slide.title }}</h3>
          <p class="caption-description">{{ slide.description }}</p>
        </div>
      </div>
    </div>
    
    <!-- 导航按钮 -->
    <button 
      class="nav-button prev" 
      @click="prevSlide"
      aria-label="上一张"
    >
      <i class="fas fa-chevron-left"></i>
    </button>
    <button 
      class="nav-button next" 
      @click="nextSlide"
      aria-label="下一张"
    >
      <i class="fas fa-chevron-right"></i>
    </button>
    
    <!-- 指示器 -->
    <div class="carousel-indicators">
      <button 
        v-for="(slide, index) in slides" 
        :key="index"
        class="indicator"
        :class="{ active: index === currentIndex }"
        @click="goToSlide(index)"
        :aria-label="`切换到第${index + 1}张`"
      ></button>
    </div>
  </div>
</template>

<script>
export default {
  name: "ourclass",
  data() {
    return {
      currentIndex: 0,
      timer: null,
      // 轮播图数据
      slides: [
        {
          imageUrl: "/1.jpg",
          altText: "校长讲话",
          title: "校长讲话",
          description: "亲爱的校长讲话"
        },
        {
          imageUrl: "/2.jpg",
          altText: "班主任风采",
          title: "班主任风采",
          description: "各班班主任风采"
        },
        {
          imageUrl: "/3.jpg",
          altText: "学生风采",
          title: "学生风采",
          description: "学生风采"
        },
        {
          imageUrl: "/4.jpg",
          altText: "校运会风采",
          title: "校运会风采",
          description: "第三届校运会"
        }
      ]
    };
  },
  methods: {
    // 下一张
    nextSlide() {
      this.currentIndex = (this.currentIndex + 1) % this.slides.length;
      this.resetTimer();
    },
    
    // 上一张
    prevSlide() {
      this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length;
      this.resetTimer();
    },
    
    // 跳转到指定幻灯片
    goToSlide(index) {
      this.currentIndex = index;
      this.resetTimer();
    },
    
    // 启动自动播放
    startAutoPlay() {
      this.timer = setInterval(() => {
        this.nextSlide();
      }, 5000); // 5秒切换一次
    },
    
    // 重置计时器
    resetTimer() {
      if (this.timer) {
        clearInterval(this.timer);
        this.startAutoPlay();
      }
    }
  },
  mounted() {
    // 组件挂载后开始自动播放
    this.startAutoPlay();
    
    // 鼠标悬停时暂停自动播放
    const container = this.$el.querySelector('.carousel-container');
    container.addEventListener('mouseenter', () => {
      if (this.timer) {
        clearInterval(this.timer);
      }
    });
    
    // 鼠标离开时恢复自动播放
    container.addEventListener('mouseleave', () => {
      this.startAutoPlay();
    });
  },
  beforeUnmount() {
    // 组件卸载前清除计时器
    if (this.timer) {
      clearInterval(this.timer);
    }
  }
};
</script>

<style scoped>
.carousel-container {
  position: relative;
  width: 100%;
  height: 100%;
  max-width: 1200px;
  margin: 0 auto;
  overflow: hidden;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  background-color: rgba(255,255,255,0.8);
}

.carousel-wrapper {
  display: flex;
  align-items: center;
  transition: all 0.5s ease;
  width: 100%;
  height: 100%;
  transition: transform 0.5s ease-in-out;
}

.carousel-slide {
  min-width: 100%;
  position: relative;
}

.slide-image {
  width: 100%;
  height: 500px;
  object-fit: cover;
  display: block;
}

.slide-caption {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 2rem;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.8), transparent);
  color: white;
  transform: translateY(20px);
  opacity: 0;
  transition: all 0.5s ease;
}

.carousel-slide:hover .slide-caption {
  transform: translateY(0);
  opacity: 1;
}

.caption-title {
  font-size: 1.8rem;
  margin-bottom: 0.5rem;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

.caption-description {
  font-size: 1rem;
  max-width: 80%;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}

.nav-button {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background-color: rgba(255, 255, 255, 0.3);
  border: none;
  color: white;
  font-size: 1.5rem;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  z-index: 10;
}

.nav-button:hover {
  background-color: rgba(255, 255, 255, 0.7);
  color: #333;
}

.prev {
  left: 1rem;
}

.next {
  right: 1rem;
}

.carousel-indicators {
  position: absolute;
  bottom: 1rem;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  gap: 0.5rem;
  z-index: 10;
}

.indicator {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 2px solid white;
  background-color: transparent;
  cursor: pointer;
  transition: all 0.3s ease;
}

.indicator.active {
  background-color: white;
  transform: scale(1.2);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .slide-image {
    height: 350px;
  }
  
  .caption-title {
    font-size: 1.4rem;
  }
  
  .caption-description {
    font-size: 0.9rem;
    max-width: 100%;
  }
  
  .nav-button {
    width: 40px;
    height: 40px;
    font-size: 1rem;
  }
}

@media (max-width: 480px) {
  .slide-image {
    height: 250px;
  }
  
  .slide-caption {
    padding: 1rem;
  }
  
  .caption-title {
    font-size: 1.2rem;
  }
  
  .caption-description {
    display: none;
  }
}
</style>
